package com.smartum.mpr.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartum.mpr.model.News;

@Repository
public class NewsDao extends GenericDao<Long, News> {

	public List<News> getPublicNews(Long timestamp, int amount) {
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		criteria.addOrder(Order.desc("timeStamp"));
		if (timestamp == 0) {
			criteria.add(Restrictions.gt("timeStamp", timestamp));
		} else {
			criteria.add(Restrictions.lt("timeStamp", timestamp));
		}
		return hibernateTemplate.findByCriteria(criteria, 0, amount);
	}

	public List<News> getUserNews(Long timestamp, int amount, long userId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		criteria.addOrder(Order.desc("timeStamp"));
		if (timestamp == 0) {
			criteria.add(Restrictions.gt("timeStamp", timestamp)).createCriteria("owner")
			        .add(Restrictions.eq("id", userId));
		} else {
			criteria.add(Restrictions.lt("timeStamp", timestamp)).createCriteria("owner")
			        .add(Restrictions.eq("id", userId));
		}
		return hibernateTemplate.findByCriteria(criteria, 0, amount);
	}

	@Override
	protected Class<News> getEntityClass() {
		return News.class;
	}

}
